﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace PloobsEngine.SceneControl
{
    public class FinalCombination : IDeferredFinalCombination
    {
        private QuadRender quadRenderer;                
        private Effect finalCombineEffect;
        private EngineStuff engine;
        private bool toTexture = true;              
        private RenderTarget2D target;    //this Render Target will hold color and Specular Intensity
        private static String NormalTechnincName = "NormalTechnich";
        private string currentTechnic;
        private Color ambientColor = Color.Black;

        public Color AmbientColor
        {
            get { return ambientColor; }
            set { ambientColor = value; }
        }
        
        #region IDeferredFinalCombination Members

        public void DrawScene(QuadRender render, GameTime gameTime,IWorld world, IDeferredGBuffer DeferredGBuffer, IDeferredLightMap DeferredLightMap)
        {
            EngineStuff.GraphicsDevice.RenderState.AlphaBlendEnable = false;
            EngineStuff.GraphicsDevice.RenderState.DepthBufferWriteEnable = false;
            EngineStuff.GraphicsDevice.RenderState.DepthBufferEnable = false;
            //EngineStuff.GraphicsDevice.Clear(ClearOptions.DepthBuffer | ClearOptions.Target, Color.TransparentBlack, 1, 0);
                        
            finalCombineEffect.Begin();
            {
                finalCombineEffect.CurrentTechnique.Passes[0].Begin();
                finalCombineEffect.Parameters["EXTRA1"].SetValue(DeferredGBuffer[GBufferTypes.Extra1]);
                finalCombineEffect.Parameters["ambientColor"].SetValue(ambientColor.ToVector3());
                finalCombineEffect.Parameters["colorMap"].SetValue(DeferredGBuffer[GBufferTypes.COLOR]);                
                finalCombineEffect.Parameters["lightMap"].SetValue(DeferredLightMap[DeferredLightMapType.LIGHTMAP]);
                finalCombineEffect.Parameters["halfPixel"].SetValue(EngineStuff.HalfPixel);                
                render.Render(Vector2.One * -1, Vector2.One);                
                finalCombineEffect.CurrentTechnique.Passes[0].End();

            }
            finalCombineEffect.End();  

            if (SaveToTexture)
            {
                EngineStuff.GraphicsDevice.SetRenderTarget(0, null);
            }
            
        }

        public void LoadContent(EngineStuff engine , bool useFloatingBuffer)
        {
            currentTechnic = NormalTechnincName;            
            this.engine = engine;
            quadRenderer = new QuadRender(EngineStuff.GraphicsDevice);            
            finalCombineEffect = EngineStuff.InternalContentManager.GetAsset<Effect>("CombineFinal");

            if (SaveToTexture)
            {
                if (useFloatingBuffer)
                    target = EngineStuff.GetDefaultBuffer(SurfaceFormat.HalfVector4);
                else
                    target = EngineStuff.GetDefaultColorBuffer();
            }            
        }

        #endregion

        #region IDeferredFinalCombination Members

        public void SetFinalCombination()
        {
            if (SaveToTexture)
            {
                EngineStuff.GraphicsDevice.SetRenderTarget(0, target);                
            }
            else
            {
                EngineStuff.GraphicsDevice.SetRenderTarget(0, null);
                EngineStuff.GraphicsDevice.Clear(ClearOptions.DepthBuffer | ClearOptions.Target, Color.TransparentBlack, 1, 0);
            }
        }

        #endregion

        #region IDeferredFinalCombination Members


        public bool SaveToTexture
        {
            get
            {
                return this.toTexture;
            }
            set
            {
                this.toTexture = value;
            }
        }

        #endregion

        #region IDeferredFinalCombination Members


        public Texture2D this[GBufferTypes type]
        {
            get
            {
                if (type == GBufferTypes.FINALIMAGE)
                {
                    return this.target.GetTexture();
                }
                else
                {
                    throw new Exception("wrong type");
                }
            }
        }

        #endregion
    }
}
